// -----------------------------------------------------------------------
//  <copyright file="DeleteLoginLogCommand.cs" company="LiuliuSoft">
//      Copyright (c) 2022-2025 DaprPlus. All rights reserved.
//  </copyright>
//  <site>https://dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2025/1/15 10:35</last-date>
// -----------------------------------------------------------------------

using Liuliu.Fsb.Services.Identity.Application.Identity.LoginLogs.Specs;
using Liuliu.Fsb.Services.Identity.Domain.Entities;

namespace Liuliu.Fsb.Services.Identity.Application.Identity.LoginLogs.Commands;

[AuthConfig(typeof(LoginLog), ResourceAccessType.RoleLimit, "Delete")]
public class DeleteLoginLogCommand(long[] ids) : IRequest<ApiResult>, ICommand
{
    public long[] Ids { get; set; } = ids;
}

public class DeleteLoginLogCommandHandler(IRepository<LoginLog> repository)
    : IRequestHandler<DeleteLoginLogCommand, ApiResult>
{
    public async Task<ApiResult> Handle(DeleteLoginLogCommand request, CancellationToken cancellationToken)
    {
        var spec = new LoginLogByIdsSpec(request.Ids);
        var count = await repository.DeleteBatchAsync(spec, cancellationToken);
        return ApiResult.SuccessOrNoChanged(count > 0, $"{count}个登录日志删除成功");
    }
}

public class DeleteLoginLogCommandValidator : AbstractValidator<DeleteLoginLogCommand>
{
    public DeleteLoginLogCommandValidator()
    {
        RuleFor(m => m.Ids).NotEmpty().WithMessage("要删除的登录日志编号不能为空");
    }
}
